package cn.itcast.service;

import cn.itcast.interfaces.MenuService;
import cn.itcast.mapper.MenuMapper;
import cn.itcast.pojo.Menu;
import com.alibaba.dubbo.config.annotation.Service;
import io.reactivex.Observable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: peterChen
 * @date: 2019/8/19 22:23
 * @description: TODO
 * @version: 1.0
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private  MenuMapper menuMapper;

    /**
     * 根据用户姓名获取对应权限下的菜单
     * @param username
     * @return
     */
    @Override
    public List<Menu> getMenuByUserName(String username) {

        List<Menu> menus = menuMapper.getMenuByUserName(username);
        if(!CollectionUtils.isEmpty(menus) && menus.size() > 0){
            /*for (Menu menu : menus) {
                List<Menu> menuList = menuMapper.findChildMenusByParentMenuId(menu.getId());
                menu.setChildren(menuList);
            }*/
            //Rxjava遍历
            Observable.fromIterable(menus).subscribe(menu -> {
                List<Menu> menuList = menuMapper.findChildMenusByParentMenuId(menu.getId());
                menu.setChildren(menuList);
            });
        }
        return menus;

    }
}
